package crypto

import (
	"crypto/rc4"
	"encoding/base64"
)

type RC4 struct {
	key []byte
}

func NewRC4Crypto(key []byte) *RC4 {
	return &RC4{
		key: key,
	}
}

func (r *RC4) EncryptWithBase64(data []byte) string {
	cipher1, _ := rc4.NewCipher(r.key)
	cipher1.XORKeyStream(data, data) // 加密后的数据直接覆盖到str中
	result := base64.URLEncoding.EncodeToString(data)
	return result
}
func (r *RC4) DecryptWithBase64(data string) (string, error) {
	str, err := base64.URLEncoding.DecodeString(data)
	if err != nil {
		return "", nil
	}
	cipher2, err := rc4.NewCipher(r.key)
	if err != nil {
		return "", nil
	}
	cipher2.XORKeyStream(str, str) // 解密后的数据直接覆盖到str中
	return string(str), nil
}
